package com.util;

import javafx.util.converter.LocalDateStringConverter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class CustomDateConverter extends LocalDateStringConverter {
    private static final DateTimeFormatter CUSTOM_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public CustomDateConverter() {
        super();
    }

    @Override
    public String toString(LocalDate date) {
        if (date == null) {
            return "";
        }
        return date.format(CUSTOM_FORMATTER);
    }

    @Override
    public LocalDate fromString(String string) {
        if (string == null || string.isEmpty()) {
            return null;
        }
        try {
            return LocalDate.parse(string, CUSTOM_FORMATTER);
        } catch (Exception e) {
            // 可以在这里处理日期转换错误，例如弹出错误消息
            return null;
        }
    }
}
